﻿using SimpleInjector;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.Entities.Universes;
using Sustainalytics.DataLayer.ConnectionStrings;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Universes.BusinessLogic;
using System.Configuration.Abstractions;

namespace Sustainalytics.Universes.Service.Infrastructure
{
    public static class CompositionRoot
    {
        public static Container RegisterDependencies(System.Web.Http.HttpConfiguration config)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();
            container.RegisterWebApiControllers(config);

            container.RegisterMisc();

            container.RegisterCollections();

            container.RegisterRepositories();

            container.Verify();
            return container;
        }

        private static void RegisterCollections(this Container container)
        {
            container.Register(typeof(IRepositoryCollection<UniverseCompany>), () => new RepositoryCollection<UniverseCompany>(MongoFactory.GetConnectionStrings().ClientUniverses));
        }

        private static void RegisterRepositories(this Container container)
        {
            container.Register(typeof(IUniversesRepository), () => UniversesRepository.GetUniversesRepository(MongoFactory.GetConnectionStrings().ClientUniverses));
        }

        private static void RegisterMisc(
            this Container container
            )
        {
            container.Register(() => MongoFactory.GetConnectionStrings());
            container.RegisterSingleton(typeof(IUniversesManagement), () => UniversesManagement.GetInstance(container.GetInstance<IUniversesRepository>()));
        }
    }
}
